package com.zhouyong.dubboFramework.proxy;

import com.zhouyong.dubboFramework.http.HttpClientUtil;
import com.zhouyong.dubboFramework.pojo.Invocation;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理工厂（使用jdk的方式）
 *
 * @Author 99
 * @Date 2022/7/27 17:25
 * @Version 1.0
 */
public class ProxyFactory {

    public static <T> T createProxy(Class<?> interfaceClass) {
        Object instance = Proxy.newProxyInstance(ProxyFactory.class.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Invocation invocation = new Invocation(interfaceClass.getSimpleName(), method.getName(), new Class[]{String.class},
                        args);
                HttpClientUtil httpClientUtil = new HttpClientUtil();
                String result = httpClientUtil.send("localhost", 8080, invocation);
                return result;
            }
        });
        return (T) instance;
    }
}


